package com.eos.orm.c3p0.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.eos.common.tool.CoreKeyContext;
import com.eos.orm.c3p0.dao.AbstractC3p0Dao;
import com.eos.orm.c3p0.dao.DBConnectionManager;
import com.eos.orm.mybatis.model.SysCustomerLink;
import com.hf.utils.StringUtil;

/**
 * 
 * @author sz
 * 
 */
public class SysCustomerLinkDaoImpl extends AbstractC3p0Dao<SysCustomerLink> {

	/**
	 * 查询的条件： 
	 */
	public String condition1(Map<String, String> map) {
		StringBuilder condition = new StringBuilder();
		if (null == map)
			return condition.append("1=0").toString();
		if (!StringUtil.isEmpty(map.get("id")))
			condition.append("id='").append(map.get("id")).append("' and ");
		if (!StringUtil.isEmpty(map.get("deal_flag")))
			condition.append("deal_flag='").append(map.get("deal_flag"))
					.append("' and ");
		if (!StringUtil.isEmpty(map.get("customer_id")))
			condition.append("customer_id='").append(map.get("customer_id"))
					.append("' and ");
		if (!StringUtil.isEmpty(map.get("link_name")))
			condition.append("link_name like '").append(map.get("link_name")).append(
					"%' and ");
		if (!StringUtil.isEmpty(map.get("status")))
			condition.append("status=").append(map.get("status")).append(
					" and ");
		return condition.append("1=1").toString();
	}

	@Override
	public List<SysCustomerLink> findPage(Map<String, String> map) {
		List<SysCustomerLink> templist = new ArrayList<SysCustomerLink>();
		if (null == map)
			return templist;
		Connection connection = DBConnectionManager.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sqlTemp = CoreKeyContext.getCoreKey("tabs_page_sqlscript");
		String sql = sqlTemp.replace("$table_name$", map.get("table_name"));
		sql = sql.replace("$condition1$", condition1(map)) 
			.replace("$condition2$", condition2(map));
		try {
			pstmt = connection.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				SysCustomerLink temp = new SysCustomerLink();
				temp.setId(rs.getInt("id"));
				temp.setCustomerId(rs.getInt("customer_id"));
				temp.setJobDuty(rs.getString("job_duty"));
				temp.setLinkFax(rs.getString("link_fax"));
				temp.setLinkJob(rs.getString("link_job"));
				temp.setLinkMobile(rs.getString("link_mobile"));
				temp.setLinkTelephone(rs.getString("link_telephone"));
				temp.setLinkName(rs.getString("link_name"));
				temp.setLinkQq(rs.getString("link_qq"));
				temp.setLinkWx(rs.getString("link_wx"));
				temp.setStatus(rs.getString("status"));
				temp.setDealFlag(rs.getString("deal_flag"));
				templist.add(temp);
				temp = null;
			}
		} catch (SQLException e) {
			System.out
					.println("SysCustomerLinkDaoImpl exec findPage operator is fail,Exception is :"
							+ e.getMessage());
		} finally {
			freeResource(connection, pstmt, rs);
		}
		return templist;
	}
}
